﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;

/// <summary>
///UpAnddownload 的摘要说明
/// </summary>
public class UpAnddownload
{
    public static SqlConnection conn()
    {
        string sqlstr = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(sqlstr);
        return con;
    }
    SqlConnection con = conn();
	public UpAnddownload()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    /**/
    /// <summary>
    /// clsFileInput 的摘要说明。
    /// </summary>
    
        //clsODBCDB lDB;//数据库代理
        HttpPostedFile upFile;//上传的单文件
        System.Web.HttpFileCollection upFiles;//上传的多文件集合    
        string billno;
        /**/
        /// <summary>
        /// 上传下载文件类
        /// </summary>
        /// <param name="upfile">上传的单文件</param>    
        /// <param name="upfile">单据号</param>        
        public UpAnddownload(HttpPostedFile upfile, string bno)
        {
            this.upFile = upfile;
            this.billno = bno;
        }
       
        /**/
        /// <summary>
        /// 上传下载文件类
        /// </summary>
        /// <param name="upfiles">上传的多文件集合</param>        
        public UpAnddownload(System.Web.HttpFileCollection upfiles, string bno)
        {
            this.upFiles = upfiles;
            this.billno = bno;
        }
   
        private string getFileName()
        {
            FileInfo fileinfo = new FileInfo(upFile.FileName);
            return fileinfo.Name;
        }
        private string getFileExpand()
        {
            FileInfo fileinfo = new FileInfo(upFile.FileName);
            return fileinfo.Extension;
        }

        /**/
        /// <summary>
        /// 上传文件到文件夹，同时写文件原始属性到数据库
        /// </summary>        
        /// <param name="identify">识别码，以防止不同用户在同一时刻上传文件，出现相同文件名</param>
        /// <param name="filefolder">上传文件文件夹</param>
        /// <param name="tablename">记录文件原始属性的表名</param>
        /// <returns></returns>
        public bool UpFileToFolder(string identify, string filefolder, string tablename)
        {
            using (con)
            {

                try
                {
                    con.Open();
                    string _billno = billno;
                    string _filename = getFileName();
                    string _fileexpand = getFileExpand();
                    string _filealiasname = filefolder + identify + DateTime.Now.ToString("yyyyMMddhhmmfffffff") + _fileexpand;
                    string _filetype = upFile.ContentType;
                    DateTime _inputdate = DateTime.Now;
                    
                   string SqlStr = "INSERT INTO " + tablename + " (bill_no,filename,filealiasname,fileexpand,filetype,inputdate) VALUES"
                        + "(?,?,?,?,?,?)";

                   SqlCommand sqlcmd = new SqlCommand(SqlStr, con);
                   sqlcmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@billno", System.Data.SqlDbType.VarChar,20)).Value = billno;
                   sqlcmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@filename", System.Data.SqlDbType.VarChar, 50)).Value = _filealiasname;
                   sqlcmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@filealiasname", System.Data.SqlDbType.VarChar, 50)).Value = _filename;
                   sqlcmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@fileexpand", System.Data.SqlDbType.VarChar, 50)).Value = _fileexpand;
                   sqlcmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@filetype", System.Data.SqlDbType.VarChar, 50)).Value = _filetype;
                   sqlcmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@inputdate", System.Data.SqlDbType.VarChar, 50)).Value = _inputdate;

                   sqlcmd.ExecuteNonQuery();
                   //sqlcmd.BeginTrans();
                   // if (!this.lDB.ExeCmd())
                   //     return false;
                    upFile.SaveAs(_filealiasname);
                    //this.lDB.Commit();
                    return true;
                }

                catch (Exception ex)
                {
                    //if (this.lDB.TransFlag) this.lDB.Rollback();
                    return false;
                }
                finally
                {
                    con.Close();
                }
            }
        }

        /// <summary>
        /// 上传多文件到文件夹
        /// </summary>
        /// <param name="identify">识别码，以防止不同用户在同一时刻上传文件，出现相同文件名</param>
        /// <param name="filefolder">上传文件文件夹</param>
        /// <param name="tablename">记录文件原始属性的表名</param>
        /// <returns></returns>
        public bool upFilesToFolder(string identify, string filefolder, string tablename)
        {
            try
            {
                string _bill_no = billno;
                string _upurl = filefolder;
                string _tablename = tablename;
                //遍历File表单元素
                for (int iFile = 0; iFile < upFiles.Count; iFile++)
                {
                    //检查文件扩展名字
                    HttpPostedFile postedFile = upFiles[iFile];
                    string fileName;
                    fileName = System.IO.Path.GetFileName(postedFile.FileName);
                    //如果文件名不为空,上传
                    if (fileName != "")
                    {
                        this.upFile = postedFile;

                        if (!UpFileToFolder(identify, _upurl, _tablename))
                            throw new Exception();
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

